/**
 *  Stream Recorder: Records RealPlayer(tm) streams as MP3s
 *  Copyright (C) 2007  David Griffiths
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

package dg.radio2ipod;

/**
 * General &quot;kitchen drawer&quot; class of useful methods.
 * @author davidg
 */
public final class Util {

    /**
     * Prevent external extantiation of this class.
     */
    private Util() {
    }

    /**
     * Capitalize the initial letter.
     * @param s string to be formatted
     * @return copy of the input string, but with an initial capital letter.
     */
    public static String initCap(final String s) {
        String s1 = " " + s.trim().toLowerCase();
        String result = s1;
        int pos1 = s1.indexOf(' ');
        while (pos1 != -1) {
            result = result.substring(0, pos1 + 1);
            if (pos1 + 2 < s1.length()) {
                result += s1.substring(pos1 + 1, pos1 + 2).toUpperCase()
                + ((pos1 + 2 < s1.length()) ? s1.substring(pos1 + 2) : "");
            }
            pos1 = s1.indexOf(' ', pos1 + 1);
        }
        return result.trim();
    }

    /**
     * Display the base name of a Unix path. Equivalent of the basename shell
     * function. For exmaple &quot;/usr/local/fred.sh&quot; becomes
     * &quot;fred.sh&quot;.
     * @param u path to be trimmed down
     * @return the base name of the filename
     */
    public static String getBase(final String u) {
        if (u == null) {
            return null;
        }
        int pos = u.lastIndexOf('/');
        if (pos != -1) {
            return u.substring(pos + 1);
        }
        return u;
    }

    /**
     * Get the basename minus the extension. For exmaple
     * &quot;/usr/local/fred.sh&quot; becomes &quot;fred&quot;.
     * @param u path to be converted
     * @return trimmed down filename
     */
    public static String getBaseName(final String u) {
        String s = getBase(u);
        if (s == null) {
            return null;
        }
        int pos = s.lastIndexOf('.');
        if (pos != -1) {
            return s.substring(0, pos);
        }
        return s;
    }
    
    /**
     * Display an error message.
     *@param error - string to display.
     */
    public static void errorMessage(final String error) {
        Main.getMainFrame().consoleWrite(error);
        System.err.println(error);
    }
}
